// modules/orderModules/pages/orderDetail/index.js
import {confirmOrder, findOrder} from '@/api/order'
import { msgTip, msgTipAlert } from '@/utils/MsgTipApi'
Page({

  /**
   * 组件的初始数据
   */
  data: {
    orderDetail:{},
    activeNames:['1'],
    steps:[
      {
        desc: '买家下单',
      },
      {
        desc: '待商家确认',
      },
      {
        desc: '商家确认',
      },
      {
        desc: '待发货',
      },
      {
        desc: '已发货',
      },
      {
        desc: '待收货',
      },
      {
        desc: '完成',
      }
    ],
    active:0
  },
  afterSales(){
    if(this.data.orderDetail.orderStatus == 7){
      return;
    }
  },
  /**
   * 前往商品详情页面
   */
  goGoodDeatil(event){
    let {id} = event.currentTarget.dataset;
    console.log(id);
    wx.navigateTo({
      url: `/modules/goodModules/pages/goodDetail/index?goodId=${id}`,
    })
  },
  /**
   * 申请退款
   */
  async refund(){
    if(this.data.orderDetail.orderStatus == 7){
      return
    }
    let flag = await msgTipAlert({title:'',content:'是否申请退货？'})
    if(flag){
      console.log('前往退款界面');
    }
  },
  /**
   * 再来一单
   * @param {*} params 
   */
  buyAgain(event){
    let {id} = event.currentTarget.dataset;
    console.log(id);
  },
  /** 
   * 查询订单详情
  */
  async findOrder(params){
    let res = await findOrder(params)
    this.setData({
      orderDetail: res,
      active: res.orderStatus
    })
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },

  /**
   * 确认收货
   */
  async sureReceived(event){
    if(this.data.orderDetail.orderStatus == 6){
      msgTip({title:'已收货'})
      return;
    }
    let {id} = event.currentTarget.dataset;
    let clickFlag = await msgTipAlert({title:'',content:'是否确认已经收到该商品'})
    if(clickFlag){
      let res = await confirmOrder(id)
      let orderId = this.data.orderDetail.id;
      this.setData({orderDetail:{}})
      this.findOrder(orderId);
    }
  },
  /**
   * 当页面被加载
   */
  onLoad(options){
    let {orderId} = options
    this.findOrder(orderId)
  }
})